package cm.iccsoft.android.telecomcost;

import cm.iccsoft.android.telecomcost.entities.TimePeriod;
import android.app.Activity;
import android.app.Dialog;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;
import android.view.ViewGroup.LayoutParams;

public class Utility {
	public static Display getDisplay(Activity activity) {
		WindowManager w = activity.getWindowManager();
		Display d = w.getDefaultDisplay();
		return d;
	}

	public static void setDialogLayout(Dialog dialog) {
		Activity activity = dialog.getOwnerActivity();
		Display activityDisplay = activity.getWindowManager()
				.getDefaultDisplay();

		Log.i("Telecomcost", "Setting dialog layout...total screen width = "
				+ activityDisplay.getWidth());

		int newDialogWidth = activityDisplay.getWidth() * 90 / 100;
		// int newDialogHeigth = activityDisplay.getHeight() * 90 / 100;
		Log.i("Telecomcost",
				"We set the dialog with to 90% of the screen width = "
						+ newDialogWidth);
		dialog.getWindow().setLayout(newDialogWidth, LayoutParams.WRAP_CONTENT);
	}
	
	public static void setDialogLayout(Dialog dialog,int layoutParams,int percentOfScreen) {
		percentOfScreen = percentOfScreen > 100 ? 100 : percentOfScreen;
		Activity activity = dialog.getOwnerActivity();
		Display activityDisplay = activity.getWindowManager()
				.getDefaultDisplay();

		Log.i("Telecomcost", "Setting dialog layout...total screen width = "
				+ activityDisplay.getWidth());

		int newDialogWidth = activityDisplay.getWidth() * percentOfScreen / 100;
		// int newDialogHeigth = activityDisplay.getHeight() * 90 / 100;
		Log.i("Telecomcost",
				"We set the dialog with to " + percentOfScreen + "% of the screen width = "
						+ newDialogWidth);
		dialog.getWindow().setLayout(newDialogWidth, layoutParams);
	}

	public static String quote(String str) {
		return "'" + str + "'";
	}

	public static String quote(long lng) {
		return "'" + lng + "'";
	}

	public static TimePeriod getTimePeriodFromIndex(int index) {
		switch (index) {
		case 0:
			return TimePeriod.HOUR;
		case 1:
			return TimePeriod.DAY;
		case 2:
			return TimePeriod.WEEK;
		case 3:
			return TimePeriod.MONTH;
		default:
			throw new RuntimeException("Unknown index:" + index);
		}
	}

	public static void setDialogLayout(Dialog dialog, int layoutParams) {		
		Activity activity = dialog.getOwnerActivity();
		Display activityDisplay = activity.getWindowManager()
				.getDefaultDisplay();

		Log.i("Telecomcost", "Setting dialog layout...total screen width = "
				+ activityDisplay.getWidth());

		int newDialogWidth = activityDisplay.getWidth() * 90 / 100;
		// int newDialogHeigth = activityDisplay.getHeight() * 90 / 100;
		Log.i("Telecomcost",
				"We set the dialog with to 90% of the screen width = "
						+ newDialogWidth);
		dialog.getWindow().setLayout(newDialogWidth,layoutParams);
	}
}
